package com.cq.hd.common.utils;

import org.apache.commons.lang3.StringUtils;

public class PrivacyUtil {

    public static void main(String[] args) {
        System.out.println(encryptMobile("18912341234"));
    }

    /**
     * 身份证号码：前六后四，如：340121********2341
     */
    public static String encryptIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            return "";
        }

        return replaceBetween(idCard, 6, idCard.length() - 4, null, 4);
    }

    /**
     * 银行卡号：显示前六后四
     */
    public static String encryptBankAccount(String bankAccount) {
        if (StringUtils.isEmpty(bankAccount)) {
            return "";
        }

        return replaceBetween(bankAccount, 4, bankAccount.length() - 4, null, 4);
    }

    /**
     * 手机号：显示前三后四
     */
    public static String encryptMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return "";
        }

        return replaceBetween(mobile, 3, mobile.length() - 4, null, 4);
    }

    public static String encryptRealName(String realName) {
        if (StringUtils.isEmpty(realName)) {
            return "";
        }

        return replaceBetween(realName, 1, realName.length(), null, 2);
    }

    /**
     * 将字符串开始位置到结束位置之间的字符用指定字符替换
     */
    private static String replaceBetween(String sourceStr, int begin, int end, String replacement, int replaceLength) {
        if (sourceStr == null) {
            return "";
        }

        if (replacement == null) {
            replacement = "*";
        }

//        int replaceLengthTmp = end - begin;
//        if (replaceLength > replaceLengthTmp) {
//            replaceLength = replaceLengthTmp;
//        }

        if (StringUtils.isNotBlank(sourceStr) && replaceLength > 0) {
            StringBuilder sb = new StringBuilder(sourceStr);
            sb.replace(begin, end, StringUtils.repeat(replacement, replaceLength));
            return sb.toString();
        }
        return sourceStr;
    }

}
